﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Basmus.Conel
{
    public class CacheBinder : IBinder
    {
        private readonly IBinder _binder;

        public CacheBinder(IBinder binder)
        {
            _binder = binder;
        }

        private readonly ConcurrentDictionary<Type, Binding> _cache = new ConcurrentDictionary<Type, Binding>();

        public Binding Bind(Type containerType)
        {
            var binding=_cache.GetOrAdd(containerType, (type) => _binder.Bind(type));
            return binding;
        }

        private readonly ConcurrentDictionary<Tuple<Type, Type>, Binding> _cache2 = new ConcurrentDictionary<Tuple<Type, Type>, Binding>();

        public Binding Bind(Type containerType, Type elementType)
        {
            var binding= _cache2.GetOrAdd(new Tuple<Type, Type>(containerType, elementType),
                             (typePair) => _binder.Bind(typePair.Item1, typePair.Item2));
            return binding;
        }
    }
}
